local util = require "util"
local conf = require "conf"
local require = require
local setmetatable = setmetatable
local error = error
local table = table
local ngx = ngx
local os = os
local string = string
local tonumber = tonumber
package.cpath = "/usr/nginx/?.so;;"
local json = require 'cjson'

module(...)
_VERSION = '0.1'


function search(user, args)
    local db = conf.get_mysql()
    local res = nil
    local count = nil
    if id then
        res = db:query("SELECT * from `role` WHERE id = " .. args.id)
    else
        res = db:query("SELECT * from `role`")
    end
    return res
end

function searchController(user, args)
    local res = search(user, args)
    if not res then
        ngx.print(json.encode({
            err = 0,
            msg = {}
        }))
        ngx.exit(200)
    else
        ngx.print(json.encode({
            err = 0,
            msg = res
        }))
        ngx.exit(200)
    end
end

function default(args)
    local conf = require "conf"
    local user = conf.parse_token(args, "role")
    -- conf.check_permissions(uid, "role")
    if args.action == 'search' then
        searchController(user, args)
    end
end

local class_mt = {
    __newindex = function (table, key, val)
        error('attempt to write to undeclared variable "' .. key .. '"')
    end
}
setmetatable(_M, class_mt)
